import { TeamOrder } from "../../config/play_def";
import { CardSkill, CommonBuff, RoleBuff, RoleSkill } from "../../config/role_def";
import { UI_Layer } from "../../config/ui_def";

cc.Class({
    extends: cc.Component,

    properties: {
        icon_sprite: cc.Sprite,          // 技能图标
        cd_cover_sprite: cc.Sprite,     // cd遮罩
        success_node: cc.Node,
        using_node: cc.Node,             // 使用中
        icon_widget: cc.Widget,

        cd_count: 0,                    // 冷却计数
        cd_time: 3,                    // 冷却所需时间
        cd_over: false,                 // 冷却结束

        use_count: 0,                    // 持续中计数
        use_time: 3,                     // 持续时间
        using: false,                // 是否使用中

        lock: true,                     // 是否解锁
    },
    initInfo(info) {
        if (info) {
            this.info = info
            this.card_info = he.db.getDataByArgs("Card", { cardId: info.id, level: info.lv })
            this.icon_sprite.spriteFrame = he.load.get("skill", cc.SpriteAtlas).getSpriteFrame(this.card_info.cardId)
            this.cd_cover_sprite.node.active = true
            this.icon_widget.updateAlignment()
            this.lock = false
        } else {
            this.info = null
            this.card_info = null
            this.lock = true
            this.icon_sprite.spriteFrame = he.load.get("skill", cc.SpriteAtlas).getSpriteFrame('skill_kong')
            this.cd_cover_sprite.node.active = false
        }
    },
    click() {
        if (this.cd_over && !this.using) {
            this.cd_count = 0           // 切换为持续计数
            if (this.card_info.swapOut && this.card_info.backtime) {
                let swap_list = this.card_info.swapOut.split("#")       // 需要角色
                let swap_in_list = []                                   // 换入角色
                if (this.card_info.swapIn) {
                    swap_in_list = this.card_info.swapIn.split("#")
                }
                let role_list = []                              // 场上已有角色
                for (let key in gg.control.roleScript) {
                    let role_script = gg.control.roleScript[key]
                    if (!role_script.out_status && role_script.role_id) {
                        role_list.push(role_script.role_id)
                    }
                }
                let swap_status = true  // 替换角色
                for (let i = 0; i < swap_list.length; i++) {
                    if (role_list.indexOf(parseInt(swap_list[i])) < 0) {
                        swap_status = false
                    }
                }
                if (swap_status) {
                    for (let i = 0; i < swap_list.length; i++) {
                        for (let key in gg.control.roleScript) {
                            let role_script = gg.control.roleScript[key]
                            if (role_script.role_id == parseInt(swap_list[i])) {
                                role_script.swapOut(this.card_info.backtime)
                            }
                        }
                    }
                } else {
                    gg.uiManager.addTip(`条件未达成，无法使用技能`)
                    return
                }
                gg.uiManager.addPage(UI_Layer.Pop, {
                    name: "SkillDebut",
                    debut: this.card_info.debut,
                    swap_list: swap_list,
                    swap_in_list: swap_in_list
                })
                this.use_time = this.card_info.backtime          // 使用时间
                this.using = true
                this.using_node.active = true
                this.success_node.active = false
            } else {
                this.restartCd()
            }
            this.happenEffect()
        }
    },

    changeRole() {
        if (this.card_info.swapOut && this.card_info.backtime) {
            let swap_list = this.card_info.swapOut.split("#")       // 需要角色
            let swap_in_list = []                                   // 换入角色
            if (this.card_info.swapIn) {
                swap_in_list = this.card_info.swapIn.split("#")
            }
            let role_list = []                              // 场上已有角色
            for (let key in gg.control.roleScript) {
                let role_script = gg.control.roleScript[key]
                if (!role_script.out_status && role_script.role_id) {
                    role_list.push(role_script.role_id)
                }
            }
            let swap_status = true  // 替换角色
            for (let i = 0; i < swap_list.length; i++) {
                if (role_list.indexOf(parseInt(swap_list[i])) < 0) {
                    swap_status = false
                }
            }
            if (swap_status) {
                for (let i = 0; i < swap_list.length; i++) {
                    for (let key in gg.control.roleScript) {
                        let role_script = gg.control.roleScript[key]
                        if (role_script.role_id == parseInt(swap_list[i])) {
                            role_script.swapOut(this.card_info.backtime)
                        }
                    }
                }
            } else {
                gg.uiManager.addTip(`条件未达成，无法使用技能`)
                return
            }
            gg.uiManager.addPage(UI_Layer.Pop, {
                name: "SkillDebut",
                debut: this.card_info.debut,
                swap_list: swap_list,
                swap_in_list: swap_in_list
            })
            this.use_time = this.card_info.backtime          // 使用时间
            this.using = true
            this.using_node.active = true
            this.success_node.active = false
        }
        this.happenEffect()
    },
    restartCd() {
        this.using_node.active = false
        this.cd_over = false
        this.cd_count = 0
    },
    // 使用技能
    happenEffect() {
        // console.log(this.card_info)
        if (!this.card_info.skill) {
            return
        }
        let card_param
        if (this.card_info.skillParam) {
            card_param = this.card_info.skillParam.split("/")
        }
        let swap_list
        switch (this.card_info.skill) {
            case CardSkill.CreateBullet:
                swap_list = this.card_info.swapOut.split("#")
                for (let i = 0; i < swap_list.length; i++) {
                    let role_info = gg.data.getRoleByRoleID(swap_list[i])
                    let role_order = TeamOrder.indexOf(role_info.order)
                    let role_pos = gg.control.roleScript[role_order + 1].getWorldPos()

                    let bullet_voice = ''
                    if (card_param[7]) {
                        bullet_voice = 'bullet/' + card_param[7]
                    }
                    let car_bullet = {
                        pos: role_pos,
                        damege: parseInt(card_param[6]),                // 伤害
                        dir: cc.v2(0, 1),               // 方向
                        script: card_param[0],            // 子弹脚本
                        voice: bullet_voice,       // 子弹声音
                        through: new Boolean(card_param[3]),
                        speed: parseInt(card_param[5]),       // 子弹速度
                        collider: card_param[2], // 碰撞形状
                        style: {                             // 子弹样式
                            skin: card_param[4],
                            url: 'spine/bullet/' + card_param[1],
                            type: sp.SkeletonData
                        },
                        effect: [],                          // 子弹效果
                    }
                    // 位置#角色_等级#装备_等级
                    car_bullet = gg.bulletManager.addBulletEffect(car_bullet, this.card_info.extra)
                    car_bullet.attack_from = `${role_info.order}#${role_info.id}_${role_info.equips[role_info.hand]}#${this.card_info.cardId}_${this.card_info.level}`
                    gg.bulletManager.addBullet(car_bullet)
                }
                break
            case CardSkill.CreateDelayBullet:
                swap_list = this.card_info.swapOut.split("#")
                let missiles_num = card_param.shift()
                let missiles_time = card_param.shift()
                let missiles_extra = this.card_info.extra
                for (let i = 0; i < swap_list.length; i++) {
                    let role_info = gg.data.getRoleByRoleID(swap_list[i])
                    let role_order = TeamOrder.indexOf(role_info.order)
                    let role_pos = gg.control.roleScript[role_order + 1].getWorldPos()
                    let MissilesBuff = {
                        key: CommonBuff.CreateBullet,
                        pos: role_pos,
                        num: parseInt(missiles_num),
                        time: parseFloat(missiles_time),
                        tcount: parseFloat(missiles_time),
                        param: card_param,
                        extra: missiles_extra,
                        attack_from: `${role_info.order}#${role_info.id}_${role_info.equips[role_info.hand]}#${this.card_info.cardId}_${this.card_info.level}`
                    }
                    gg.control.addBuff(MissilesBuff)
                }
                break
            case CardSkill.CreateDecorate:
                swap_list = this.card_info.swapOut.split("#")
                let decorate_num = card_param.shift()
                let decorate_time = card_param.shift()
                let decorate_extra = this.card_info.extra
                for (let i = 0; i < swap_list.length; i++) {
                    let role_info = gg.data.getRoleByRoleID(swap_list[i])
                    let role_order = TeamOrder.indexOf(role_info.order)
                    let role_pos = gg.control.roleScript[role_order + 1].getWorldPos()
                    let MissilesBuff = {
                        key: CommonBuff.CreateDecorate,
                        pos: role_pos,
                        num: parseInt(decorate_num),
                        time: parseFloat(decorate_time),
                        tcount: parseFloat(decorate_time),
                        param: card_param,
                        extra: decorate_extra,
                        attack_from: `${role_info.order}#${role_info.id}_${role_info.equips[role_info.hand]}#${this.card_info.cardId}_${this.card_info.level}`
                    }
                    gg.control.addBuff(MissilesBuff)
                }
                break
        }
    },
    // 更新 cd值
    updateCd(dt) {
        if (this.cd_over) {
            return
        }
        this.cd_count += dt
        if (this.cd_count > this.cd_time) {
            this.cd_over = true
            this.cd_cover_sprite.fillRange = 0
            this.success_node.active = true
        } else {
            this.cd_cover_sprite.fillRange = 1 - (this.cd_count / this.cd_time)
        }
    },
    // 更新 使用时间
    updateUse(dt) {
        if (this.using) {
            this.use_count += dt
            if (this.use_count > this.use_time) {
                this.using = false
                this.use_count = 0
                this.restartCd()
            }
        }
    },
    update(dt) {
        if (!this.lock && gg.data.status && !gg.data.pause) {
            this.updateCd(dt)
            this.updateUse(dt)
        }
    }
});
